<?php

define("DEFAULT_ROWS_PER_PAGE", 100);

function project_list_rows($where, $orderby = "", $limit = "")
{
    global $dpdb;

    if(! $limit)
        $limit = "";
    $sql = project_search_view_sql($where, $orderby, $limit);
    $rows = $dpdb->SqlRows($sql);
    return $rows;
}

function project_search_view_sql($where, $orderby = "", $limit = "")
{
    $where = trim($where);
    if(! empty($where))
    {
        $where = "WHERE \n".$where;
    }
    return  "SELECT
                p.projectid,
                p.nameofwork AS title,
                p.authorsname AS author,
                p.language,
                p.other_languages,
                p.difficulty,
                p.genre,
                p.state,
                p.sequence_code,
                p.username as pm,
                SUM( 
                    CASE WHEN pg.state LIKE '%".PAGE_AVAIL."'
                        THEN 1 ELSE 0 END
                ) AS pages_avail,
                SUM( 
                    CASE WHEN pg.state LIKE '%".PAGE_OUT."'
                        THEN 1 ELSE 0 END
                    ) AS pages_out,
                SUM( 
                    CASE WHEN pg.state LIKE '%".PAGE_TEMP."'
                        THEN 1 ELSE 0 END
                    ) AS pages_temp,
                SUM( 
                    CASE WHEN pg.state LIKE '%".PAGE_SAVED."'
                        THEN 1 ELSE 0 END
                    ) AS pages_saved,
                COUNT(pg.projectid) AS pages_total,
                SUM( 
                    CASE WHEN pg.state LIKE '%".PAGE_BAD."'
                        THEN 1 ELSE 0 END
                    ) AS pages_bad,
                COUNT(pg.fileid) AS pages_total,
                p.username AS project_manager,
                p.checkedoutby,
                pm.bb_id AS pm_bbid,
                CASE WHEN p.state LIKE 'P1%' THEN 1
                     WHEN p.state LIKE 'P2a%' THEN 2
                     WHEN p.state LIKE 'P2b%' THEN 3
                     WHEN p.state LIKE 'P2c%' THEN 4
                     WHEN p.state LIKE 'P3%'  THEN 5
                     WHEN p.state LIKE 'F1a%' THEN 6
                     WHEN p.state LIKE 'F1b%' THEN 7
                     WHEN p.state LIKE 'F1c%' THEN 8
                     WHEN p.state LIKE 'F2a%' THEN 9
                     WHEN p.state LIKE 'F2b%' THEN 10
                     ELSE 11
                END AS round_index,
                cob.bb_id AS cob_bbid
            FROM
                projects AS p
            JOIN
                pages AS pg ON p.projectid = pg.projectid
            LEFT JOIN
                users AS pm ON p.username = pm.username
            LEFT JOIN
                users as cob ON p.checkedoutby = cob.username
            $where
            GROUP BY
                p.projectid
            $orderby";
            // $limit";
}

function title_link($title, $row)
{
    global $code_url;
    $url = "$code_url/project.php"
        ."?projectid={$row['projectid']}'";
    return "<a href='{$url}>{$title}</a>";
}

function page_counts($row)
{
    global $pm_url;
    $url = url_for_page_list($row['projectid']);
    // $url = "$pm_url/page_detail.php"
            // ."?projectid={$row['projectid']}";
            
    $fmt = "<a href='{$url}'>%d-%d-%d<br />(%d-%d)</a>";
    // if($row['pages_avail'] == 0)
    // {
        $fmt = "<span style='color: pink'>$fmt</span>";
    // }
    return sprintf($fmt, $row['pages_avail'],
         $row['pages_out'] + $row['pages_temp'],
         $row['pages_saved'], $row['pages_bad'],
         $row['pages_total']);
}

function pmlink($pm, $row)
{
    global $Context;
    return $Context->LinkForPMToUser($pm);
}

function coblink($cob, $row)
{
    global $Context;
    if(empty($cob))
        return "";
    return $Context->LinkForPMToUser($cob);
}

function round_button($roundid, $row)
{
    global $show;
    $ret = $roundid;
    $next = ThisUrl();

    $projectid = $row['projectid'];
    // $roundid = $row['roundid'];
    $k = mb_strpos($row['state'], ".");
    $roundid = left($row['state'], $k);
    $seq_code = $row['sequence_code'];
    $nextroundid = RoundIdAfter($roundid, $seq_code);
    $status = substr($row['state'], $k+1);
    $state = $row['state'];
    if($row['pages_saved'] == 0
        || $row['pages_saved'] != $row['pages_total'])
    {
        return $roundid;
    }
    return "$roundid
    <form name='ra_$projectid' accept-charset='UTF-8' method='POST'>
        <input type='hidden' name='projectid' value='$projectid'>
        <input type='hidden' name='roundid'   value='$roundid'>
        <input type='hidden' name='status'  value='$status'>
        <input type='hidden' name='todo' value='advance'>
        <input type='hidden' name='show' value='$show'>
        <input type='button' name='radv_$projectid' value='$nextroundid'
            onclick='this.form.submit()'>
    </form>\n";
// <input type="button" value="Home Page" onclick="window.location.href='http://www.computerhope.com'"> 
}


function status_button($row)
{
    global $code_url;
    $projectid = $row['projectid'];
    $status = $row['status'];
    $url_changestate = "{$code_url}/tools/changestate.php"
                        ."?projectid=$projectid"
                        ."&amp;curr_state=$status";
    return "$status&nbsp;<input class='right' type='button' value='»'
                    onclick='window.location.href=\"".$url_changestate."\"'>\n";
// <input type="button" value="Home Page" onclick="window.location.href='http://www.computerhope.com'"> 
}

function status_combo($row)
{
    global $code_url;
    $projectid = $row['projectid'];

    $opts = "
        <select name='next_state' onchange='this.form.submit()'>\n";
    switch($row['status'])
    {
        case "unavail":
            $opts .= "
            <option value='proj_unavail' SELECTED>unavailable</option>
            <option value='proj_waiting'>waiting</option>
            <option value='proj_avail'>avail</option>\n";
            break;
        case "avail":
            $opts .= "
            <option value='proj_unavail'>unavailable</option>
            <option value='proj_waiting'>waiting</option>
            <option value='proj_avail' SELECTED>avail</option>\n";
            break;
        case "waiting":
            $opts .= "
            <option value='proj_unavail'>unavailable</option>
            <option value='proj_waiting' SELECTED>waiting</option>
            <option value='proj_avail'>avail</option>\n";
            break;
        case "completed":
            $opts .= "
            <option value='proj_completed' SELECTED>completed</option>
            <option value='next_round'>next round</option>
            \n";
            break;
    }
    $opts .= "
        </select>\n";

    $status = $row['status'];
    $roundid = $row['roundid'];
    $url_changestate = "$code_url/tools/changestate.php";

    $s = "
    <form action='$url_changestate' 
                        accept-charset='UTF-8' method='POST'>
        <input type='hidden' name='projectid' value='$projectid'>
        <input type='hidden' name='curr_state' value='proj_$status'>
        <input type='hidden' name='show' value='$show'>
        <input type='hidden' name='roundid' value='$roundid'>\n";

    $s .= $opts;
    $s .= "
    </form>\n";
    return $s;
}

function words_link($row)
{
    global $wc_url;
    $projectid = $row['projectid'];
    return "<a target='_blank' 
                href='$wc_url/project_words.php"
                    ."?projectid=$projectid'>"
                    ._("Words")."</a>\n";
}

function edit_link($row)
{
    global $pm_url;
    global $User;
    if($User->IsSiteManager() 
        || $User->IsProjectFacilitator()
        || $row['pm'] == $User->Username())
    {
        return "<a target='_blank' 
                    href='$pm_url/editproject.php"
                        ."?action=edit"
                        ."&amp;projectid={$row['projectid']}'>"
                        ._("Edit")."</a>";
    }
    else
    {
        return "";
    }
}
?>
